package kr.pe.johnkim.checkmileage.mobile.rest.controller;

import kr.pe.johnkim.checkmileage.mobile.domain.CheckMileageLog;
import kr.pe.johnkim.checkmileage.mobile.rest.service.CheckMileageLogService;
import kr.pe.johnkim.checkmileage.mobile.util.AES256Cipher;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.AlgorithmParameters;

/**
 * Check Mileage 로그 관련 Controller 클래스 이다.
 * 
 * @author : johnkim <cale9797@gmail.com / Bettersoft, Inc.>
 * @Date : 12. 7. 23
 */

@Controller
@Path("/checkMileageLogController")
public class CheckMileageLogController {

    // SLF4J Logging
	private Logger logger = LoggerFactory.getLogger(CheckMileageLogController.class);

    @Autowired
    private CheckMileageLogService checkMileageLogService = null;

    @Autowired
    private AES256Cipher aes256Cipher = null;

    /**
     * 로그를 저장 한다.
     *
     * 1. parameter01 : 사용자 전화번호.
     * 2. parameter02 : 위도.
     * 3. parameter03 : 경도.
     * 4. parameter04 : 검색일 경우 검색어.
     * 5. parameter05 : 예비용도.
     * 
     * @param is
     * @throws Exception
     */
    @POST
    @Path("/registerLog")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public void registerLog(InputStream is) throws Exception {
        logger.info("registerLog");

        String key = "Created_by_JohnKim_in_Bettersoft";
//        String key = "Created_by_JohnK";

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonLog = new JSONObject(line);
        JSONObject jsonLog2 = jsonLog.getJSONObject("checkMileageLog");

        CheckMileageLog parameterLog = new CheckMileageLog();

        parameterLog.setCheckMileageId(jsonLog2.getString("checkMileageId"));
        parameterLog.setMerchantId(jsonLog2.getString("merchantId"));
        parameterLog.setViewName(jsonLog2.getString("viewName"));
        parameterLog.setParameter01(jsonLog2.getString("parameter01"));
        parameterLog.setParameter02(jsonLog2.getString("parameter02"));
        parameterLog.setParameter03(jsonLog2.getString("parameter03"));
        parameterLog.setParameter04(jsonLog2.getString("parameter04"));
        parameterLog.setParameter05(jsonLog2.getString("parameter05"));
        parameterLog.setParameter06(jsonLog2.getString("parameter06"));
        parameterLog.setParameter07(jsonLog2.getString("parameter07"));
        parameterLog.setParameter08(jsonLog2.getString("parameter08"));
        parameterLog.setParameter09(jsonLog2.getString("parameter09"));
        parameterLog.setParameter10(jsonLog2.getString("parameter10"));
        parameterLog.setRegisterDate(jsonLog2.getString("registerDate"));

        logger.info("checkMileageId : {}", parameterLog.getCheckMileageId());
        logger.info("merchantId     : {}", parameterLog.getMerchantId());
        logger.info("viewName       : {}", parameterLog.getViewName());
        logger.info("parameter01    : {}", parameterLog.getParameter01());
        logger.info("parameter02    : {}", parameterLog.getParameter02());
        logger.info("parameter03    : {}", parameterLog.getParameter03());
        logger.info("registerDate   : {}", parameterLog.getRegisterDate());

        checkMileageLogService.registerLog(parameterLog);

        parameterLog.setCheckMileageId(jsonLog2.getString("checkMileageId"));
        parameterLog.setMerchantId(jsonLog2.getString("merchantId"));
        parameterLog.setViewName(jsonLog2.getString("viewName"));
        parameterLog.setParameter01(jsonLog2.getString("parameter01"));
//        parameterLog.setParameter02(aes256Cipher.AES_Decode(jsonLog2.getString("parameter02"), key));
//        parameterLog.setParameter03(aes256Cipher.AES_Decode(jsonLog2.getString("parameter03"), key));
        parameterLog.setParameter02(jsonLog2.getString("parameter02"));
        parameterLog.setParameter03(jsonLog2.getString("parameter03"));
        parameterLog.setParameter04(jsonLog2.getString("parameter04"));
        parameterLog.setParameter05(jsonLog2.getString("parameter05"));
        parameterLog.setParameter06(jsonLog2.getString("parameter06"));
        parameterLog.setParameter07(jsonLog2.getString("parameter07"));
        parameterLog.setParameter08(jsonLog2.getString("parameter08"));
        parameterLog.setParameter09(jsonLog2.getString("parameter09"));
        parameterLog.setParameter10(jsonLog2.getString("parameter10"));
        parameterLog.setRegisterDate(jsonLog2.getString("registerDate"));

        logger.info("checkMileageId : {}", parameterLog.getCheckMileageId());
        logger.info("merchantId     : {}", parameterLog.getMerchantId());
        logger.info("viewName       : {}", parameterLog.getViewName());
        logger.info("parameter01    : {}", parameterLog.getParameter01());
        logger.info("parameter02    : {}", parameterLog.getParameter02());
        logger.info("parameter03    : {}", parameterLog.getParameter03());
        logger.info("registerDate   : {}", parameterLog.getRegisterDate());

        checkMileageLogService.registerDecodeLog(parameterLog);
    }

    /**
     * 암/복호화 테스트용.
     *
     * @param is
     * @throws Exception
     */
    @POST
    @Path("/testLog")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public void testLog(InputStream is) throws Exception {

        logger.info("registerLog");

        String key = "Created_by_JohnKim_in_Bettersoft";
//        String key = "Created_by_JohnK";

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonLog = new JSONObject(line);
        JSONObject jsonLog2 = jsonLog.getJSONObject("checkMileageLog");

        CheckMileageLog parameterLog = new CheckMileageLog();

        parameterLog.setParameter02(aes256Cipher.AES_Decode(jsonLog2.getString("parameter02"), key));
        parameterLog.setParameter03(aes256Cipher.AES_Decode(jsonLog2.getString("parameter03"), key));

        logger.info("parameter02    : {}", parameterLog.getParameter02());
        logger.info("parameter03    : {}", parameterLog.getParameter03());
    }
}

